package _59_之字形打印二叉树;

import java.util.ArrayList;
import java.util.LinkedList;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

/*
题目描述
请实现一个函数按照之字形打印二叉树，即第一行按照从左到右的顺序打印，第二层按照从右至左的顺序打印，第三行按照从左到右的顺序打印，其他行以此类推。
 */
public class Solution {
    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (pRoot == null) return res;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offerLast(pRoot);
        int size = 0;
        boolean flag = true;

        while (!queue.isEmpty()) {
            size = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode t = queue.pollFirst();
                if (flag == true) {
                    list.add(t.val);
                } else {
                    list.add(0, t.val);
                }
                if (t.left != null) queue.offerLast(t.left);
                if (t.right != null) queue.offerLast(t.right);
            }
            res.add(list);
            flag = !flag;
        }
        return res;
    }

    public static void main(String[] args) {
        boolean f = true;
        f = !f;
        System.out.println(f);
    }
}
